package com.sysu.minsysu.zdkb.utils;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;

public class CourseProvider extends ContentProvider{
	
	private static final String TAG = "CalProvider";
	
	public static final String URIPath = "com.sysu.minsysu.zdkb.courseprovider";
	
	ArrayList<Course>[] mCourses;
	ArrayList<CourseItem> mCourseItems = new ArrayList<CourseItem>();
	
	private static final String _ID = "_id";
	private static final String COURSENAME= SearchManager.SUGGEST_COLUMN_TEXT_1;
	private static final String COURSEID = SearchManager.SUGGEST_COLUMN_INTENT_DATA;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		Log.d(TAG, "Provider Created");
		mCourses = CourseUtils.getClassTimetable(getContext(), "classes.xml");
		int _id = 0;
		for( int i=0; i<mCourses.length; i++ ){
			for( Course course : mCourses[i] ){
				mCourseItems.add( new CourseItem( _id++ , course.courseName.toLowerCase(), course.id ));
			}
		}
		
		final Collator temp = Collator.getInstance(Locale.CHINA);
	
		Comparator<CourseItem> comparator = new Comparator<CourseItem>() {

			@Override
			public int compare(CourseItem object1, CourseItem object2) {
				return temp.compare(object1.courseName, object2.courseName);
			}
			
		};
		
		Collections.sort(mCourseItems, comparator);
		
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		//Log.d(TAG, uri.toString());
		//String queryText = uri.getLastPathSegment().toLowerCase();
		//Log.d(TAG, "queryText " + queryText);
		//Log.d(TAG, "selection " + selection);
		
		MatrixCursor cursor = new MatrixCursor( new String[]{ _ID, COURSENAME, COURSEID });
		if( selectionArgs==null ){
			for( int i=0; i<5; i++ ){
				CourseItem item = mCourseItems.get(i);
				cursor.addRow( new Object[]{ item.id, item.courseName, item.id});
			}
		}else{
			String queryText = selectionArgs[0];
			if( queryText.equals(""))
				return null;
			
			queryText.toLowerCase();
			Log.d(TAG, "queryText = " + queryText);
			for( CourseItem item : mCourseItems ){
				if( item.courseName.contains(queryText)){
					Log.d(TAG, "add Course = " + item.courseName);
					cursor.addRow( new Object[]{ item.id, item.courseName, item.courseId});
				}
			}
		}
		cursor.moveToFirst();
		return cursor;
	}
	

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	class CourseItem{
		public CourseItem( long id, String name, long courseId ){
			this.id = id; courseName = name; this.courseId = courseId;
		}
		public long id;
		public String courseName;
		public long courseId;
	}
}
